package com.wmh.baseservice.operationaid.controller.product;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.web.UserUtil;
import com.wmh.baseservice.operationaid.entity.ProductMonitor;
import com.wmh.baseservice.operationaid.pojo.vo.product.monitor.ProductMonitorCreateReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.product.monitor.ProductMonitorPageReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.product.monitor.ProductMonitorUpdateReqVO;
import com.wmh.baseservice.operationaid.service.ProductMonitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;

/**
 * @author 谢骏
 * @date 2023/9/25 11:48
 **/
@Validated
@RestController
@RequestMapping("operationAid/product/monitor")
@Api(tags = "运营助手-商品-监控单元")
public class ProductMonitorController {

    @Resource
    private ProductMonitorService productMonitorService;

    @Resource
    private UserUtil userUtil;

    @ApiModelProperty("创建")
    @PostMapping("create")
    public String create(@RequestBody @Valid ProductMonitorCreateReqVO reqVO) {
        return success(productMonitorService.create(reqVO));
    }

    @ApiModelProperty("删除")
    @PostMapping("delete")
    public String delete(@NotNull(message = "未提交主键") @Size(min = 1, message = "未提交主键") @RequestBody List<Long> ids) {
        productMonitorService.delete(ids);
        return success();
    }

    @ApiModelProperty("修改")
    @PostMapping("update")
    public String update(@RequestBody @Valid ProductMonitorUpdateReqVO reqVO) {
        return success(productMonitorService.update(reqVO));
    }

    @ApiModelProperty("分页")
    @GetMapping("page")
    public String page(@Valid ProductMonitorPageReqVO reqVO) {
        boolean superAdmin = userUtil.isSuperAdmin();
        PageResult<ProductMonitor> result = new PageUtil<ProductMonitor>(reqVO).startPage((page, wrapper) -> {
            LambdaQueryWrapper<ProductMonitor> l = wrapper.lambda();
            if (superAdmin) {
                l.eq(reqVO.getAdId() != null, ProductMonitor::getAdId, reqVO.getAdId());
            } else {
                l.eq(ProductMonitor::getAdId, reqVO.getAdId());
            }
            l.eq(reqVO.getId() != null, ProductMonitor::getId, reqVO.getId());
            l.eq(reqVO.getProductId() != null, ProductMonitor::getProductId, reqVO.getProductId());
            l.like(StrUtil.isNotBlank(reqVO.getDescription()), ProductMonitor::getDescription, reqVO.getDescription());
            productMonitorService.page(page, wrapper);
        });
        return success(result);
    }

}
